package lab12;

public class Employee {

	private static final String DEFAULT_NAME = "Juan Amigo Maria Rodgriguez";
	private static final int MAX_WORKING_HOURS = 8;

	private String name;
	private Task currentTask;
	private int hoursLeft;
	private static AllWork allwork;

	public Employee(String name) {
		if (name != null && !name.equals("")) {
			this.name = name;
		} else {
			this.name = DEFAULT_NAME;
		}

		hoursLeft = MAX_WORKING_HOURS;
	}

	/**
	 * @return the currentTask
	 */
	public Task getCurrentTask() {
		return currentTask;
	}

	/**
	 * @param currentTask
	 *            the currentTask to set
	 */
	public void setCurrentTask(Task currentTask) {
		if (currentTask != null)
			this.currentTask = currentTask;
	}

	/**
	 * @return the hoursLeft
	 */
	public int getHoursLeft() {
		return hoursLeft;
	}

	/**
	 * @param hoursLeft
	 *            the hoursLeft to set
	 */
	public void setHoursLeft(int hoursLeft) {
		if (hoursLeft >= 0 && hoursLeft <= MAX_WORKING_HOURS)
			this.hoursLeft = hoursLeft;
	}

	/**
	 * @return the allwork
	 */
	public static AllWork getAllwork() {
		return allwork;
	}

	/**
	 * @param allwork
	 *            the allwork to set
	 */
	public static void setAllwork(AllWork allwork) {
		if (allwork != null)
			Employee.allwork = allwork;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	void work() {
		System.out.println("Employee named " + this.getName()
				+ " starts working.");
		while (getHoursLeft() > 0 && !allwork.isAllWorkDone()) {
			if (currentTask == null || currentTask.getWorkingHours() == 0) {
				currentTask = allwork.getNextTask();
				if(currentTask==null) return;
					System.out.println("Assigning task "
							+ currentTask.getName() + " to " + this.getName()
							+ ".");
			}

			if (currentTask != null) {
				int hoursToWorkOnThisTask = Math.min(
						currentTask.getWorkingHours(), hoursLeft);
				setHoursLeft(getHoursLeft() - hoursToWorkOnThisTask);
				currentTask.setWorkingHours(currentTask.getWorkingHours()
						- hoursToWorkOnThisTask);
				if(currentTask==null) return;
				System.out.println("Employee named " + this.getName()
						+ " works on task " + this.currentTask.getName()
						+ " for " + hoursToWorkOnThisTask + " hours.");
			}
			
			
		}
	}

	void startWorkingDay() {
		hoursLeft = MAX_WORKING_HOURS;
	}

}
